package com.biz.primus.ms.order.convertor;

import com.biz.primus.base.enums.order.OrderTypeEnum;
import com.biz.primus.common.utils.StringUtil;
import com.biz.primus.model.order.vo.aftersale.QueryOrderResponseVO;
import com.biz.primus.model.order.vo.request.OrderItemRequestVO;
import com.biz.primus.model.order.vo.request.OrderRequestVO;
import com.biz.primus.model.order.vo.response.*;
import com.biz.primus.model.promotion.vo.req.OrderPromotionReqDetailVo;
import com.biz.primus.model.promotion.vo.req.OrderPromotionReqItemVo;
import com.biz.primus.ms.order.dao.po.Order;
import com.biz.primus.ms.order.dao.po.OrderItem;
import com.biz.primus.ms.order.dao.po.embeddable.OrderConsigneeInfo;
import com.google.common.collect.Lists;
import org.codelogger.utils.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单转换器
 *
 * @author Tiger Feng
 * @date 2017年11月03日
 */
public class OrderConvertor {

    public static Order respVO2PO(OrderResponseVO vo,
                                  Long userId,
                                  String depotCode,
                                  String orderIp,
                                  OrderConsigneeInfo consignee,
                                  OrderPromotionResponseVO promotion) {
        if (vo == null) {
            return null;
        }
        Order po = new Order();
        BeanUtils.copyProperties(vo, po);
        po.setUserId(userId);
        po.setDepotCode(depotCode);
        po.setOrderIp(orderIp);
        po.setConsignee(consignee);
        if (promotion != null && StringUtil.isExistStr(promotion.getOrderCode())) {
            if (promotion.getOrderCode().equals(po.getOrderCode())) {
                po.setPromotion(OrderPromotionConvertor.detailVO2PO(promotion));
            }
        }
        if (vo.getItems() != null && !vo.getItems().isEmpty()) {
            List<OrderItem> orderItems = new ArrayList<>(vo.getItems().size());
            for (OrderItemResponseVO itemResponseVO : vo.getItems()) {
                orderItems.add(OrderItemConvertor.responseVO2PO(itemResponseVO, po));
            }
            po.setItems(orderItems);
        }
        return po;
    }

    public static OrderResponseVO po2ResponseVO(Order po) {
        if (po == null) {
            return null;
        }
        OrderResponseVO vo = new OrderResponseVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    /**
     * OrderVO 转 OrderPromotionReqDetailVo，获取订单促销信息时的入参
     *
     * @param orderVO
     * @param dayOrderNo
     * @return
     */
    public static OrderPromotionReqDetailVo orderRequestVO2PromotionReqDetailVO(OrderRequestVO orderVO, Integer dayOrderNo) {
        OrderPromotionReqDetailVo orderPromotionReqDetailVo = new OrderPromotionReqDetailVo();
        orderPromotionReqDetailVo.setDayOrderNo(dayOrderNo);
        orderPromotionReqDetailVo.setParamSeq(orderVO.getOrderCode());
        switch (orderVO.getOrderType()) {
            case flash:
                orderPromotionReqDetailVo.setOrderType(OrderTypeEnum.O2O);
                break;
            case store:
                orderPromotionReqDetailVo.setOrderType(OrderTypeEnum.MALL);
                break;
            case seckill:
                orderPromotionReqDetailVo.setOrderType(OrderTypeEnum.MALL);
                break;
            default:
                break;
        }
        List<OrderPromotionReqItemVo> items = Lists.newArrayList();
        if (orderVO.getItems() != null && !orderVO.getItems().isEmpty()) {
            for (OrderItemRequestVO orderItemVO : orderVO.getItems()) {
                items.add(OrderConvertor.orderItemRequestVO2OrderPromotionReqItemVO(orderItemVO));
            }
        }
        orderPromotionReqDetailVo.setItems(items);
        return orderPromotionReqDetailVo;
    }

    /**
     * OrderItemVO 转 OrderPromotionReqItemVo，获取订单促销信息转化请求参数
     *
     * @param orderItemVO
     * @return
     */
    public static OrderPromotionReqItemVo orderItemRequestVO2OrderPromotionReqItemVO(OrderItemRequestVO orderItemVO) {
        OrderPromotionReqItemVo orderPromotionReqItemVo = new OrderPromotionReqItemVo();
        orderPromotionReqItemVo.setProductId(orderItemVO.getProductId());
        orderPromotionReqItemVo.setQuantity(orderItemVO.getQuantity());
        orderPromotionReqItemVo.setCategoryId(orderItemVO.getCategoryId());
        orderPromotionReqItemVo.setBrandId(orderItemVO.getBrandId());
        orderPromotionReqItemVo.setOriginPrice(orderItemVO.getMemberPrice());
        orderPromotionReqItemVo.setSpecialOfferPrice(orderItemVO.getSpecialOfferPrice());
        orderPromotionReqItemVo.setSpecialOfferQuantityLimit(orderItemVO.getSpecialOfferQuantityLimit());
        return orderPromotionReqItemVo;
    }

    public static QueryOrderResponseVO order2ApplyForAfterSaleOrderResponseVO(Order order) {
        if (order == null) {
            return null;
        }
        QueryOrderResponseVO vo = new QueryOrderResponseVO();
        BeanUtils.copyProperties(order, vo);
        vo.setItems(OrderItemConvertor.pos2VOs(order.getItems()));
        return vo;
    }

}
